package co.fitstart.admin.web.controller.trade.product;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.admin.web.interceptor.MenuSetting;

import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.service.ProductService;
import co.fitstart.entity.product.Product;
import co.fitstart.entity.product.ProductCategory;

/**
 * 
 * @author lidan
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/products")
@MenuSetting("menu-ecommerce")
public class ProductEditController {
    
    private static final Logger logger = LoggerFactory.getLogger(ProductEditController.class);
    
    @Autowired
    private ProductService productService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<ProductCategory> productCategoryService;
    
    /**
     * View product
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}", method = RequestMethod.GET)
    @MenuSetting("submenu-product-management")
    public ModelAndView view(@PathVariable("productId") Long productId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-view");
        
        Product product = productService.load(Product.class, productId);
        
        modelAndView.addObject("product", product);
        
        return modelAndView;
    }
    
    /**
     * Display edit panel
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("productId") Long productId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-edit");
        
        Product product = productService.load(Product.class, productId);
        
        modelAndView.addObject("product", product);
        
        return modelAndView;
    }
    
    /**
     * Do edit
     * 
     * @param product
     * @param inputTags
     * @return
     */
    @RequestMapping(value = "/{productId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("productId") Product product,
                                       @RequestParam(value = "inputTags", required = false) List<String> inputTags) {
        
        Result result = new Result();
        
        try {
            
            productService.edit(product.edit(), inputTags);
            
            result.success().message("修改产品信息成功!");
            
        } catch(Exception e) {
            
            logger.error("#onEdit - ", e);
            
            result.fail().message("修改产品信息失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display edit cover panel
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/cover", method = RequestMethod.GET)
    public ModelAndView editCover(@PathVariable("productId") Long productId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-cover-edit");
        
        modelAndView.addObject("productId", productId);
        
        return modelAndView;
    }
    
    /**
     * Do edit cover
     * 
     * @param productId
     * @param cover
     * @return
     */
    @RequestMapping(value = "/{productId}/cover", method = RequestMethod.PATCH)
    public @ResponseBody String onEditCover(@PathVariable("productId") Long productId,
                                            @RequestParam("cover") String cover){
        
        Result result = new Result();
        
        try {
            
            Product product = productService.load(Product.class, productId);
            
            productService.update(product.editCover(cover));
            
            result.success().message("修改产品封面成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditCover - ", e);
            
            result.fail().message("修改产品封面失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }

    /**
     * Display edit qrcode panel
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/qrcode", method = RequestMethod.GET)
    public ModelAndView editQrcode(@PathVariable("productId") Long productId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-qrcode-edit");
        
        modelAndView.addObject("productId", productId);
        
        return modelAndView;
    }
    
    /**
     * Do edit qrcode
     * 
     * @param productId
     * @param qrcode
     * @return
     */
    @RequestMapping(value = "/{productId}/qrcode", method = RequestMethod.PATCH)
    public @ResponseBody String onEditQrcode(@PathVariable("productId") Long productId, 
                                             @RequestParam("qrcode") String qrcode ){
        
        Result result = new Result();
        
        try {
            
            Product product = productService.load(Product.class, productId);
            
            productService.update(product.editQrcode(qrcode));
            
            result.success().message("修改产品二维码成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditQrcode - ", e);
            
            result.fail().message("修改产品二维码失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display edit panel
     * 
     * @param productId
     * @return
     */
    @RequestMapping(value = "/{productId}/description/edit", method = RequestMethod.GET)
    @MenuSetting("submenu-product-management")
    public ModelAndView editDescription(@PathVariable("productId") Long productId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/product-description-edit");
        
        Product product = productService.load(Product.class, productId);
        
        modelAndView.addObject("product", product);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param productId 
     * @return
     */
    @RequestMapping(value = "/{productId}/description")
    public @ResponseBody String readDescription(@PathVariable("productId") Long productId) {
        
        Product product = productService.load(Product.class, productId);
        
        return product.getDescription();
    }
    
    /**
     * Do edit description
     * 
     * @param productId 
     * @param description 
     * @return
     */
    @RequestMapping(value = "/{productId}/description", method = RequestMethod.PATCH)
    public @ResponseBody String onEditDescription(@PathVariable("productId") Long productId, @RequestParam("description") String description) {

        Result result = new Result();

        try {
            
            Product product = productService.load(Product.class, productId);
            
            productService.update(product.editDescription(description));

            result.success().message("修改产品详情成功！");

        } catch (Exception e) {

            logger.error("onEditDescription(): ", e);
            result.fail().message("修改产品详情失败！");

        }

        return JsonUtils.toJsonString(result);
    }

}
